﻿using Common;
using DBUtility;
using System;
using System.Data;
using System.Text;

namespace BaseDal
{
    public class SysLogDal
    {
        static readonly Lazy<SysLogDal> dal = new Lazy<SysLogDal>(() => new SysLogDal());
        public static SysLogDal Instance { get; } = dal.Value;
        private SysLogDal() { }

        private const string table = "SysLog";

        /// <summary>
        /// 查询日志
        /// </summary>
        /// <param name="pageSize">页尺寸</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序字段</param>
        /// <param name="recordCount">记录数</param>
        /// <returns></returns>
        public DataTable GetSysLog(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder sb = GetSelectSql(strWhere);
            string countSql = PageHelper.CreateCountingSql(sb.ToString());
            recordCount = Convert.ToInt32(SqlHelper.GetSingle(countSql));
            string pageSql = PageHelper.CreatePageSql(recordCount, pageSize, pageIndex, sb.ToString(), filedOrder);
            DataSet ds = SqlHelper.Query(pageSql);
            return ds.Tables[0];
        }

        /// <summary>
        /// 查询SQL语句
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public StringBuilder GetSelectSql(string strWhere = "")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT ID,LogDate,Message,Method,LoginIP,UserName,Module,Status ");
            sb.Append("FROM " + table);

            if (!string.IsNullOrEmpty(strWhere))
            {
                sb.Append(strWhere);
            }

            return sb;
        }

    }
}
